package com.hyg.lib_music.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.DataModel.Music.FavMusic;
import com.hyg.lib_music.R;

import me.drakeet.multitype.ItemViewBinder;

public class MusicFavViewBinder extends ItemViewBinder<FavMusic, MusicFavViewBinder.ViewHolder> {


    private OnMusicClickListener onMusicClickListener;

    @Override
    protected @NonNull ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_music_child, parent, false);

        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull FavMusic music) {
        holder.setData(music);
    }


    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public FavMusic music;
        private @NonNull
        ImageView play_ico;
        private @NonNull
        TextView musicName;
        private @NonNull
        ImageView fav;
        private @NonNull
        LinearLayout musicChild;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            play_ico = itemView.findViewById(R.id.play_ico);
            musicName = itemView.findViewById(R.id.musicName);
            fav = itemView.findViewById(R.id.fav);
            musicChild = itemView.findViewById(R.id.music_child);
            // 注册点击事件
            musicChild.setOnClickListener(this);
            fav.setOnClickListener(this);
            fav.setRotation(180);
        }

        void setData(@NonNull final FavMusic music) {
            this.music = music;
            musicName.setText(music.musicName);
            if(music.isWhite) {
                musicChild.setBackgroundColor(Color.WHITE);
            }else{
                musicChild.setBackgroundResource(R.drawable.button_light_gray);
            }
            if(music.isPlaying) {
                play_ico.setVisibility(View.VISIBLE);
                musicName.setTextColor(Color.parseColor("#7957a9"));
            }else{
                play_ico.setVisibility(View.GONE);
                musicName.setTextColor(Color.parseColor("#333333"));
            }
            fav.setImageResource(R.mipmap.ic_music_delete);
        }

        @Override
        public void onClick(View view) {
            if (onMusicClickListener != null) {
                int id = view.getId();
                if (id == R.id.music_child) {
                    onMusicClickListener.onItemClick(view, this.music);
                } else if (id == R.id.fav) {
                    onMusicClickListener.onItemClick(view, this.music);
                }
            }
        }
    }
    // 自定义点击事件
    public void setOnMusicClickListener(OnMusicClickListener onMusicClickListener) {
        this.onMusicClickListener = onMusicClickListener;
    }

    public interface OnMusicClickListener {
        void onItemClick(View view, FavMusic music);
    }
}
